<?php
	class configControl extends commonControl{
		/**
		 * 设置基本配置项
		 */
		public function setconfig (){
			$config_type = M('config_type');
			$con_type = $config_type->all();
    		$this->assign('con_type', $con_type); //获得配置组
    		
    		$config_db = M('config');
    		$tid = $config_db->group("tid")->field("tid")->all();//获得配置项中存在的配置组
    		$config = array();
    		foreach($tid as $v){
    			$data = $config_db->where(" tid='".$v['tid']."'")->all();
    			$data = format_config($data); //组合html
    			$config[$v['tid']] = $data;
    		}
    		$this->assign('config', $config);
			$this->display();
		}
		/**
		 * 执行批量修改配置项
		 */
		public function setconfig_exe(){
			//P($_POST);exit;
			$conf = M('config');
			foreach($_POST as $k=>$v){
				if(is_array($v)){
					$_POST[$k] = implode(',', $v);
				}
			}
			$arr = $_POST;
			foreach($arr as $k=>$v){
				$conf->where(" name = '".$k."'")->update(array('value'=>"$v"));
			}
			if($this->setconfig_addconf_putfile_exe()){
				$this->success('恭喜修改成功');
			}else{
				$this->error('修改失败,哎，可惜啊！');
			}
		}
		public function setconfig_addconf_putfile_exe(){
			$conf = M('config');
			$str = '<?php
                if (!defined("PATH_HD"))exit;
                return ';
			$config = $conf->findall();
			$config = fromDbToArr($config); //将数据取出了然后进行数组重新匹配恢复 .将数组中值需要重新转成数组，转成数组
			foreach($config as $k=>$v){
				$put_conf[$v['name']] = $v['value'];
			}
			$str.=var_export($put_conf, true) . ';?>';
			if(file_put_contents(PATH_ROOT.'/config/uconfig.php', $str)){
				return true;
			}else{
				return false;
			}
		}
		/**
		 * 显示配置项
		 */
		public function config_show(){
			$type_conf = isset($_GET['tid']) && !empty($_GET['tid'])? " bc_config.tid = '".$_GET['tid']. "'":'';
			$config = K('conf_type');
			$count = $config->where($type_conf)->count();
			$page = new page($count,11,6,2);
			$conf_type = $config->where($type_conf)->field('id,name,value,title,msg,tname,config.system,config_type.tid|typeid,config.tid')->order('tid')->limit($page->limit())->all();
			$this->assign('conf_type', $conf_type);
			$this->assign('page',$page->show());
			$this->display();
		}
		/**
		 * 添加配置项显示页面
		 */
		public function config_add_show(){
			//查出配置组
			$config_type = M('config_type');
			$con_type = $config_type->all();
			$this->assign('con_type', $con_type);
			$this->display();
		}
		/**
		 * 修改配置项
		 */
		public function config_modify(){
			//获得配置组
			$config_type = M('config_type');
			$con_type = $config_type->all();
			$this->assign('con_type', $con_type);
			//获得要修改额配置项的详细
			$conf = M('config');
			$config = $conf->where("id = '".$_GET['id']."'")->find();
			$this->assign('config', $config);
			$this->display();
		}
		/**
		 * 修改配置项
		 */
		public function config_modify_exe(){
			
			$conf = M('config');
			if($conf->where("id = '".$_POST['id']."'")->update($_POST)){
				$this->success('恭喜修改成功└(^o^)┘',__CONTROL__."/config_show");
			}else{
				$this->error('可惜了，修改失败了^_^o~ 努力！');
			}
		}
		/**
		 * 显示配置组
		 */
		public function type_show(){
			//查出配置组
			$config_type = M('config_type');
			$con_type = $config_type->all();
			$this->assign('con_type', $con_type);
			$this->display();
		}
		/**
		 * 添加配置组显示页面
		 */
		public function type_add_show(){
			$this->display();
		}
		/**
		 * 执行添加配置组
		 */
		public function type_add_exe(){
			$type = M('config_type');
			$_POST['tname'] = $_POST['conf_type'];
			if($type->add()){
				$this->success('恭喜添加成功');
			}else{
				$this->error('添加失败');
			}
		}
		/**
		 * 编辑配置组
		 */
		public function type_modify(){
			$type = M('config_type');
			$type = $type->where("tid = '".$_GET['tid']."'")->find();
			$this->assign('type', $type);
			$this->display();
		}
		/**
		 * 执行配置组修改
		 */
		public function type_modify_exe(){
			$type = M('config_type');
			if($type->update()){
				$this->success('恭喜修改成功');
			}else{
				$this->error('杯具，修改失败咯。。。。');
			}
		}
		/**
		 * 执行配置组删除
		 */
		public function type_del(){
			$type_conf = K('type_configView');
			$type = M('config_type');
			$row = $type->where("tid = '".$_GET['tid']."'")->find();
			if($row['system'] == 1){
				$this->error('系统配置组不能删除');
			}else{
				if($type->table('config')->where("tid = '".$_GET['tid']."'")->find()){
					$this->error('该配置组下还有配置选项，不能删除');
				}else{
					if($type->where("tid = '".$_GET['tid']."'")->del()){
						$this->success('恭喜删除成功');
					}else{
						$this->error('可惜，删除失败了。。');
					}
				}
			}
		}
		/**
		 * 删除配置项
		 */
		public function config_del(){
			$db = M('config');
			if($db->where("id = '".$_GET['id']."'")->del()){
				$this->success('删除成功');
			}else{
				$this->error('删除失败');
			}
		}
		/**
		 * 执行添加配置
		 */
		public function conf_add_exe(){
			$type = M('config');
			if($type->add()){
				if($this->setconfig_addconf_putfile_exe()){
					$this->success('恭喜添加成功');
				}else{
					$this->error('添加失败,哎，可惜啊！');
				}
			}else{
				$this->error('添加失败');
			}
		}
		/**
		 * 改变网站风格
		 */
		public function setstyle(){
			$tplDir = PATH_ROOT.'/template';//网站前台模版文件目录
			$default_tpl = include PATH_ROOT.'/bccms/index/config/config.php'; //用来保存网站前台的模版
			$default_tpl = $default_tpl['TPL_STYLE'];
			$tplInfo = dir::tree($tplDir);
			foreach($tplInfo as $k=>$v){
				if($v['name'] != $default_tpl){
					$tpl_info_node[] = $tplInfo[$k]; //非当前模版
				}else{
					$tpl_info_de = $tplInfo[$k]; //当前模版
				}
			}
			//非当前模版信息
			foreach($tpl_info_node as $k=>$v){
				$info = dir::tree($tplDir.'/'.$v['name']);
				foreach($info as $key=>$value){
					$filename = pathinfo($value['name']);
					if($filename['filename'] == 'preview'){
						$tpl_info_node[$k]['demopath'] = __ROOT__.'/template/'.$v['name'].'/'.$value['name'];
					}else{
						
					}
				}
			}
			//当前模版信息
			foreach($info as $key=>$value){
				$info = dir::tree($tplDir.'/'.$tpl_info_de['name']);
				$filename = pathinfo($value['name']);
				if($filename['filename'] == 'preview'){
					$tpl_info_de['demopath'] = __ROOT__.'/template/'.$default_tpl.'/'.$value['name'];
				}
			}
			$this->assign('style_default', $tpl_info_de);
			$this->assign('style_info', $tpl_info_node);
			$this->display();
		}
		/**
		 * 执行换模板
		 */
		public function setstyle_exe(){
			$style = $_GET['tplname'];
			$indexconfig = include PATH_APP_GROUP.'/index/config/config.php';
			$indexconfig['TPL_STYLE'] = $style;
			$str = '<?php
				if (!defined("PATH_HD"))exit;
					return ';
			$indexstr = $str.var_export($indexconfig,true).';?>';
			file_put_contents(PATH_APP_GROUP.'/index/config/config.php', $indexstr);
			$this->success('恭喜修改成功');
		}
	}

?>